module.exports = app => {
    const mongoose = app.mongoose,
        Schema = mongoose.Schema,
        ObjectId = Schema.Types.ObjectId;
    /**
     * @ article data schema
     */
    let ArticleSchema = new Schema({
        author: { type: ObjectId, ref: 'Member' },
        title: String,
        summary: String,
        content: String,
        banner: [{type: String}],
        label: { type: ObjectId, ref: 'Label' },
        date: { type: Number, default: Date.now },
        views: { type: Number, default: 0 },
        collect: { type: Number, default: 0 },
        rewards: [
            {
                from: { type: ObjectId, ref: 'Member' },
                amount: { type: Number, default: 0 }
            }
        ],
        isBanner: Boolean,
        comments: [{ type: ObjectId, ref: 'Comment' }],
        isRelease: { type: Boolean, default: true }
    }, {usePushEach: true});
  
    ArticleSchema.pre('save', (next) => {
        this.date = Date.now;
        this.views = 0;
        this.collect = 0;
        this.isRelease = true;
        next();
    })
  
    ArticleSchema.statics = {
        update(aid, params) {
            return this.findByIdAndUpdate({_id: aid}, {$set: params}, {new: true});
        },
        add(data) {
            return data.save();
        },
        del(aid) {
            return this.remove({_id: aid}).exec();
        },
        findAll: function(page, size, cb) {
            return this.find({isRelease: true})
                .sort({'date':-1})
                .limit(size)
                .skip(page * size)
                .populate('author', {'_id':1,'username':1,'avatarsPath':1})
                .populate('label', {'name':1})
                .exec(cb)
        },
        findByKey: function(key, total, page, cb) {
            return this.find({title:{$regex:key}})
                .limit(total)
                .skip(page)
                .populate('author', {'_id':0,'username':1,'avatarsPath':1})
                .populate('label', {'name':1})
                .exec(cb)
        },
        findById: function(id, cb) {
            return this.findOne({ _id: id })
            .populate('author', {'username':1,'avatarsPath':1})
            .populate('label', {'name':1})
            // .populate({path:'comments', populate:{path:'author',select:{'username':1,'avatarsPath':1}}})
            .exec(cb);
        },
        findAuthArticles: function(id, cb) {
            return this.find({author: id})
                .sort({'date':-1})
                .populate('author', {'_id':0,'username':1,'avatarsPath':1})
                .populate('label', {'name':1})
                .exec(cb)
        },
        findAuthLikes: function(id, cb) {
            return this.find({likes: id})
                .sort({'date':-1})
                .populate('author', {'_id':0,'username':1,'avatarsPath':1})
                .populate('label', {'name':1})
                .exec(cb)
        },
        release: function(id, cb) {
            return this.update({_id:id},{$set:{isRelease: true}}).exec(cb);
        },
        unRelease: function(id, cb) {
            return this.update({_id:id},{$set:{isRelease: false}}).exec(cb);
        },
        views: function(id, v, cb) {
            return this.update({_id: id}, {$set: {views: v+=1}}).exec(cb);
        },
        addCollect(aid, collect, cb) {
            console.log('95...', aid, collect);
            return this.findByIdAndUpdate({_id: aid}, {$set: {collect: collect + 1}}, cb);
        },
        cutCollect(aid, collect, cb) {
            return this.findByIdAndUpdate({_id: aid}, {$set: {collect: collect - 1}}, cb);
        },
        addComment(aid, comment, cb) {
            return this.findByIdAndUpdate({_id: aid}, {$push: {comments: comment}}, cb);
        }
    };
  
    return mongoose.model('Article', ArticleSchema);
}